<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px" label-position="left">
      <el-form-item label="设备ID" prop="equipmentId">
        <el-input
          v-model="queryParams.equipmentId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租赁数量" prop="rentalQuantity">
        <el-input
          v-model="queryParams.rentalQuantity"
          placeholder="请输入租赁数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类别" prop="equipmentCategory">
        <el-input
          v-model="queryParams.equipmentCategory"
          placeholder="请输入设备类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="共享状态" prop="sharingStatus">
        <el-select v-model="queryParams.sharingStatus" placeholder="请选择共享状态" clearable>
          <el-option
            v-for="dict in dict.type.sup_shared_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="租赁价格" prop="rentalPrice">
        <el-input
          v-model="queryParams.rentalPrice"
          placeholder="请输入租赁价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最小租赁期限" prop="minRentalPeriod">
        <el-input
          v-model="queryParams.minRentalPeriod"
          placeholder="请输入最小租赁期限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最大租赁期限" prop="maxRentalPeriod">
        <el-input
          v-model="queryParams.maxRentalPeriod"
          placeholder="请输入最大租赁期限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="押金金额" prop="depositAmount">
        <el-input
          v-model="queryParams.depositAmount"
          placeholder="请输入押金金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supply:sharedManagement:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['supply:sharedManagement:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['supply:sharedManagement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supply:sharedManagement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sharedManagementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一ID" align="center" prop="id" />
      <el-table-column label="设备ID" align="center" prop="equipmentId" />
      <el-table-column label="设备名称" align="center" prop="equipmentName" />
      <el-table-column label="设备图片" align="center" prop="equipmentImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.equipmentImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="租赁数量" align="center" prop="rentalQuantity" />
      <el-table-column label="设备类别" align="center" prop="equipmentCategory" />
      <el-table-column label="共享状态" align="center" prop="sharingStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sup_shared_status" :value="scope.row.sharingStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="租赁价格" align="center" prop="rentalPrice" />
      <el-table-column label="最小租赁期限" align="center" prop="minRentalPeriod" />
      <el-table-column label="最大租赁期限" align="center" prop="maxRentalPeriod" />
      <el-table-column label="押金金额" align="center" prop="depositAmount" />
      <el-table-column label="地址详情" align="center" prop="addressDetails" />
      <el-table-column label="地图视图" align="center" prop="mapView" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.mapView" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="文件附件URL" align="center" prop="fileAttachment" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supply:sharedManagement:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['supply:sharedManagement:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改共享设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="设备ID" prop="equipmentId">
          <el-input v-model="form.equipmentId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备图片" prop="equipmentImage">
          <image-upload v-model="form.equipmentImage"/>
        </el-form-item>
        <el-form-item label="租赁数量" prop="rentalQuantity">
          <el-input v-model="form.rentalQuantity" placeholder="请输入租赁数量" />
        </el-form-item>
        <el-form-item label="设备类别" prop="equipmentCategory">
          <el-input v-model="form.equipmentCategory" placeholder="请输入设备类别" />
        </el-form-item>
        <el-form-item label="共享状态" prop="sharingStatus">
          <el-select v-model="form.sharingStatus" placeholder="请选择共享状态">
            <el-option
              v-for="dict in dict.type.sup_shared_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租赁价格" prop="rentalPrice">
          <el-input v-model="form.rentalPrice" placeholder="请输入租赁价格" />
        </el-form-item>
        <el-form-item label="最小租赁期限" prop="minRentalPeriod">
          <el-input v-model="form.minRentalPeriod" placeholder="请输入最小租赁期限" />
        </el-form-item>
        <el-form-item label="最大租赁期限" prop="maxRentalPeriod">
          <el-input v-model="form.maxRentalPeriod" placeholder="请输入最大租赁期限" />
        </el-form-item>
        <el-form-item label="押金金额" prop="depositAmount">
          <el-input v-model="form.depositAmount" placeholder="请输入押金金额" />
        </el-form-item>
        <el-form-item label="地址详情" prop="addressDetails">
          <el-input v-model="form.addressDetails" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="地图视图" prop="mapView">
          <image-upload v-model="form.mapView"/>
        </el-form-item>
        <el-form-item label="文件附件URL" prop="fileAttachment">
          <file-upload v-model="form.fileAttachment"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSharedManagement, getSharedManagement, delSharedManagement, addSharedManagement, updateSharedManagement } from "@/api/supply/sharedManagement";

export default {
  name: "SharedManagement",
  dicts: ['sup_shared_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 共享设备管理表格数据
      sharedManagementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentId: null,
        equipmentName: null,
        equipmentImage: null,
        rentalQuantity: null,
        equipmentCategory: null,
        sharingStatus: null,
        rentalPrice: null,
        minRentalPeriod: null,
        maxRentalPeriod: null,
        depositAmount: null,
        addressDetails: null,
        mapView: null,
        fileAttachment: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        equipmentId: [
          { required: true, message: "设备ID不能为空", trigger: "blur" }
        ],
        equipmentName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        rentalQuantity: [
          { required: true, message: "租赁数量不能为空", trigger: "blur" }
        ],
        rentalPrice: [
          { required: true, message: "租赁价格不能为空", trigger: "blur" }
        ],
        minRentalPeriod: [
          { required: true, message: "最小租赁期限不能为空", trigger: "blur" }
        ],
        maxRentalPeriod: [
          { required: true, message: "最大租赁期限不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询共享设备管理列表 */
    getList() {
      this.loading = true;
      listSharedManagement(this.queryParams).then(response => {
        this.sharedManagementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentId: null,
        equipmentName: null,
        equipmentImage: null,
        rentalQuantity: null,
        equipmentCategory: null,
        sharingStatus: null,
        rentalPrice: null,
        minRentalPeriod: null,
        maxRentalPeriod: null,
        depositAmount: null,
        addressDetails: null,
        mapView: null,
        fileAttachment: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加共享设备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSharedManagement(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改共享设备管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSharedManagement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSharedManagement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除共享设备管理编号为"' + ids + '"的数据项？').then(function() {
        return delSharedManagement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('supply/sharedManagement/export', {
        ...this.queryParams
      }, `sharedManagement_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
